﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using ImageUploader.domain;

namespace ImageUploader.GUI
{
    public partial class LoginForm : Form
    {

        private string urlLDAP,domain,username,password;
        
        //private LDAPConnection ldapConnection;
        //private AuthenticationTypes authTypes;   

        public LoginForm()
        {
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            InitializeComponent();

            textBoxLDAPURL.Text = "LDAP://Tim-SERVER"; 
            textBoxUsername.Text = "Administrator";
            textBoxPassword.Text = "Admin54321%";
            txtDomain.Text = "DC=infodatek,DC=domein,DC=com";
            this.Text = "LDAP Login";
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            try
            {
                StaticMethods.ExecuteMethodShowLoading(delegate
                {
                    using (DirectoryEntry de = new DirectoryEntry(textBoxLDAPURL.Text + "/" + txtDomain.Text, textBoxUsername.Text, textBoxPassword.Text))//, authTypes))
                    {
                        DirectorySearcher ds = new DirectorySearcher(de);
                        ds.FindOne();
                        urlLDAP = textBoxLDAPURL.Text;
                        domain = txtDomain.Text;
                        username = textBoxUsername.Text;
                        password = textBoxPassword.Text;


                    }
                    this.DialogResult = DialogResult.OK;
                }, this);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Login failed.");
            }
        }

        public LDAPConnection LDAPConnection
        {
            get
            {
                return new LDAPConnection(urlLDAP, domain, username, password);
            }
        }

        private void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
        {
        }
        
        /*public SearchResultCollection Users
        {
            get;
            set;
        }*/
    }
}
